<?php
// $Id$

/**
 * Controller_Admin_Manager 控制器
 */
class Controller_Admin_Manager extends Controller_Abstract
{

	function actionIndex()
	{
		$this->_view['data'] = Manager::find()->getAll();
	}
	/**
	 * 管理员分组列表
	 */
	function actionGroup()
	{
		$this->_view['data'] = Mgroup::find()->getAll();
	}
	/**
	 * 管理员分组添加/修改
	 */
	function actionGroupadd()
	{
		$group_obj = null;
		$modify_id = intval($this->_context->mid);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'操作失败!');
		// 如果是修改
		if ($modify_id)
		{
			$group_obj = Mgroup::find($modify_id)->getOne();
			if (!$group_obj->id())
			{
				return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'您想要修改的信息没有找到!'));
			}
		}
		if ($this->_context->isPOST())
		{
			$value = array(
				'title'=>$_POST['title'],
				'power'=>isset($_POST['power'])?serialize($_POST['power']):null,
			);
			try {
				if ($modify_id){
					$group_obj->changeProps($value);
				}else{
					$group_obj = new Mgroup($value);
				}
				$group_obj->save();
				return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作分组成功,权限更改下次登录生效!','navTabId'=>'true','callbackType'=>'forward','forwardUrl'=>url('manager/group')));
				exit;
			}catch (Exception $e)
			{
				return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'操作失败,'.$e->getMessage()));
			}
		}
		if ($group_obj){
			$group_obj = $group_obj->toArray();
			$group_obj['power'] = unserialize($group_obj['power']);
		}
		// 数据加载
		$this->_view['functions'] = Functions::get_functions();
		$this->_view['action'] = url('manager/groupadd',array('mid'=>$modify_id));
		$this->_view['group'] = $group_obj;
	}
	/**
	 * 分组删除
	 */
	function actionGroupdel()
	{
		$delete_id = intval($this->_context->did);
		// 服务器响应
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'操作失败!');
		if ($delete_id){
			$group_obj = Mgroup::find($delete_id)->getOne();
			if ($group_obj->id())
			{
				if (count($group_obj->manager)){
					// 有用户不能删除
					$return_array['message'] = '此分组下还有管理员用户,不能删除!';
				}else{
					// 可以删除
					$group_obj->destroy();
					$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作成功!','callbackType'=>'forward','forwardUrl'=>url('manager/group'));
				}
			}else{
				$return_array['message'] = '操作失败,数据错误';
			}
		}else{
			$return_array['message'] = '操作失败,错误的请求';
		}
		return json_encode($return_array);
	}
	/**
	 * 
	 * 个人修改
	 */
	function actionPower()
	{
		$action = url('manager/power');
		$modify_id = intval($this->_context->id);
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'操作失败!');
		// 当操作为修改
		$manager_obj = false;
		if ($modify_id){
			$action = url('manager/power',array('id'=>$modify_id));;
			$manager_obj = Manager::find($modify_id)->getOne();
			if (!$manager_obj->id())
			{
				return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'您想要修改的信息没有找到!'));
			}
		}
		$add_form = new Form_Manager($action);
		if ($this->_context->isPOST())
		{
			// 参数校验
			if ($add_form->validate($_POST))
			{
				// 逻辑处理
				$form_values = $add_form->values();
				if ($modify_id && $manager_obj){
					// 修改
					if ($form_values['password'] != '' && $form_values['password'] == $form_values['password2'])
					{
						// 修改密码
						$form_values['user_pass'] = Helper_Common::system_md5($form_values['password']);
					}
					unset($form_values['user_name']);
					$manager_obj->changeProps($form_values);
					$manager_obj->save();
					if ($modify_id == $_SESSION['ADMIN_USER']['id']){
						// 修改自己
						$return_array = array('statusCode' => Helper_Dwz::DWZ_STATUS_TIME_OUT,'message'=>'修改成功,请重新登录!');
					}else{
						$return_array = array('statusCode' => Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作已成功!','callbackType'=>'closeCurrent','navTabId'=>'菜单管理');	
					}
				}else{
					// 添加
					if ($form_values['user_name'] == ''){
						return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'用户名不能为空!'));
					}elseif ($form_values['password'] == '' || $form_values['password'] != $form_values['password2'])
					{
						return json_encode(array('statusCode' => Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'您2次输入的密码不一致!'));
					}else{
						$form_values['user_pass'] = Helper_Common::system_md5($form_values['password']);
					}
					$manager_obj = new Manager($form_values);
					$manager_obj->save();
					$return_array = array('statusCode' => Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作已成功!','callbackType'=>'closeCurrent','navTabId'=>'菜单管理');
				}
			}else{
				// 参数校验返回
				if ($add_form->element('user_name')->isInvalid()){
					$return_array['message'] = $add_form->element('user_name')->errorMsg();
				}
			}
			// 返回数据
			return json_encode($return_array);
		}
		// 默认数据导入
		$add_form->element('groupid')->items = Mgroup::find()->getAll()->toHashMap('id','title');
		if ($manager_obj !== false)
		{
			$add_form->import($manager_obj->toArray());
			$add_form->remove('user_name');
		}
		$this->_view['form'] = $add_form;
		$this->_view['member'] = $manager_obj;
	}
	/**
	 * 删除管理员
	 */
	function actionDelete()
	{
		$delete_id = intval($this->_context->id);
		// 服务器响应
		$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'操作失败!');
		if ($delete_id == $_SESSION['ADMIN_USER']['id']){
			$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_ERROR,'message'=>'不能删除自己的账户!');
		}elseif ($delete_id){
			$manager_obj = Manager::find('id = ?',$delete_id)->getOne();
			if ($manager_obj->id())
			{
				// 可以删除
				Manager::meta()->deleteWhere($delete_id);
				$return_array = array('statusCode'=>Helper_Dwz::DWZ_STATUS_OK,'message'=>'操作成功!','callbackType'=>'forward','forwardUrl'=>url('manager'));
			}else{
				$return_array['message'] = '操作失败,数据错误';
			}
		}
		return json_encode($return_array);
	}
}